#include "time_manager_for_while.hpp"
extern __IO uint32_t u32SystemTick100us;
uint32_t adc1_times = 0, adc_0_5s_times = 0, time_too_long_cnt = 0, time_too_long_0_5s_times = 0;
__IO uint16_t u16SystemTime100us;
__IO uint16_t u16SystemTime200us;
__IO uint16_t u16SystemTime1ms;
__IO uint16_t u16SystemTime2ms;
__IO uint16_t u16SystemTime10ms;
__IO uint16_t u16SystemTime50ms;
__IO uint16_t u16SystemTime100ms;
__IO uint16_t u16SystemTime500ms;
TIM_TypeDef *tim_for_test;
ADC_TypeDef *adc_for_test;
void time_management::time_management_init(void)
{
    if (SysTick_Config(SystemCoreClock / 10000)) // 1000 means 1ms, 2000 means 0.5ms, 10000 means 100us
    {
        /* Capture error */
        while (1)
            ;
    }
    /* Configure the SysTick handler priority */
    NVIC_SetPriority(SysTick_IRQn, 0x2); // SysTick中断优先级设置, 0:highest priority, 3:lowest priority
}
void time_management::time_management_run(void)
{
    //    TIM_BDTRInitTypeDef TIM_BDTRInitStruct; // 20201027
    tim_for_test = TIM1;
    adc_for_test = ADC1;
    //    uint16_t u16TempBDTRStatusRead; // 20201027
    if (u32SystemTick100us > 0) // 100us loop, 100us executed once in this loop
    {
        //  TIM1->CCR4 = 0;
        u32SystemTick100us--;
        u16SystemTime100us++;
    }
    if (u16SystemTime100us > 2) // 200us loop, 200us executed once in this loop
    {

        u16SystemTime100us -= 2;
        u16SystemTime200us++;
    }
    if (u16SystemTime200us > 5) // 1ms loop, 1ms executed once in this loop
    {
        u16SystemTime200us -= 5;
        u16SystemTime1ms++;
    }
    else if (u16SystemTime1ms > 2) // 2ms loop, 2ms executed once in this loop
    {
        u16SystemTime1ms -= 2;
        u16SystemTime2ms++;
    }
    else if (u16SystemTime2ms > 5) // 10ms loop, 10ms executed once in this loop
    {
        u16SystemTime2ms -= 5;
        u16SystemTime10ms++;
    }
    else if (u16SystemTime10ms > 5) // 50ms loop,  50ms executed once in this loop
    {
        u16SystemTime10ms -= 5;
        u16SystemTime50ms++;
    }
    else if (u16SystemTime50ms > 2) // 100ms loop, 100ms executed once in this loop
    {
        //   speed_run_real = get_speed_now();
        u16SystemTime50ms -= 2;
        u16SystemTime100ms++;
    }
    else if (u16SystemTime100ms > 5) // 500ms loop,  500ms executed once in this loop
    {
        u16SystemTime100ms -= 5;
        u16SystemTime500ms++;
        LED2_TOGGLE();
    }
    else if (u16SystemTime500ms > 2)
    {
        u16SystemTime500ms -= 4;
        // motor_status_change_auto();
    }
}
